/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import controller.EditUser;
import controller.ListRoles;
import controller.RollDetail;
import controller.UserDetail;
import entities.Role;
import entities.Users;
import java.util.Date;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author QUY-THI
 */
@ManagedBean
@ViewScoped
public class UserDetailBean {

    private int userid;
    private Users detail;
    private String fullname;
    private String address;
    private Date bod;
    private String email;
    private String gender;
    private String phone;
    private List<Role> roles;
    private int role;

    public int getRole() {
        Role r = detail().getRoleID();
        return r.getRoleID();
    }

    public void setRole(int role) {
        this.role = role;
    }

    public String getFullname() {
        return detail().getFullname();
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getAddress() {
        return detail().getAddress();
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Date getBod() {
        return detail().getBod();
    }

    public void setBod(Date bod) {
        this.bod = bod;
    }

    public String getEmail() {
        return detail().getEmail();
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getGender() {
        return detail().getGender();
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getPhone() {
        return detail().getPhone();
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public List<Role> getRoles() {
        ListRoles r = new ListRoles();
        return r.getLRoles();
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    public Users getDetail() {
        return detail();
    }

    public void setDetail(Users detail) {
        this.detail = detail;
    }

    public int getUserid() {
        return userid;
    }

    public void setUserid(int userid) {
        this.userid = userid;
    }

    /**
     * Creates a new instance of UserDetailBean
     */
    public UserDetailBean() {
    }

    public Users detail() {
        UserDetail ud = new UserDetail();
        return ud.getUserDetail(userid);
    }

    public String editUser() {
        RollDetail rd = new RollDetail();
        EditUser ed = new EditUser();
        Users u = detail();
        u.setFullname(fullname);
        u.setAddress(address);
        u.setBod(bod);
        u.setEmail(email);
        u.setGender(gender);
        u.setPhone(phone);
        u.setRoleID(rd.roleDetail(role));
        boolean resultEdit = ed.resultEdit(u);
        if (resultEdit) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "", "Action complete !"));
            return "";
        }
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "Action error !"));
        return "";
    }
}
